package com.klm.easymq.example;

import com.klm.easymq.config.ConfigurationLoader;
import com.klm.easymq.config.EasyMqProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 配置加载示例
 * 演示SDK配置的加载和使用
 * 简化设计：只演示默认配置，环境配置由主应用管理
 */
@Slf4j
@Component
public class ConfigurationExample implements CommandLineRunner {
    
    private final ConfigurationLoader configurationLoader;
    private final EasyMqProperties easyMqProperties;
    
    public ConfigurationExample(ConfigurationLoader configurationLoader, 
                               EasyMqProperties easyMqProperties) {
        this.configurationLoader = configurationLoader;
        this.easyMqProperties = easyMqProperties;
    }
    
    @Override
    public void run(String... args) throws Exception {
        log.info("=== Easy-MQ SDK 配置加载示例 ===");
        
        // 1. 演示SDK内部配置文件加载
        demonstrateConfigLoading();
        
        // 2. 演示配置属性使用
        demonstratePropertiesUsage();
        
        // 3. 演示配置优先级
        demonstrateConfigPriority();
    }
    
    /**
     * 演示SDK内部配置文件加载
     */
    private void demonstrateConfigLoading() {
        log.info("1. 加载SDK内部配置文件...");
        
        // 加载默认配置
        Map<String, Object> defaultConfig = configurationLoader.loadDefaultConfig();
        log.info("   默认配置加载成功，配置项数量: {}", defaultConfig.size());
        
        // 检查easy-mq配置
        @SuppressWarnings("unchecked")
        Map<String, Object> easyMqConfig = (Map<String, Object>) defaultConfig.get("easy-mq");
        if (easyMqConfig != null) {
            log.info("   easy-mq配置存在，enabled: {}", easyMqConfig.get("enabled"));
            
            // 检查幂等控制配置
            @SuppressWarnings("unchecked")
            Map<String, Object> idempotentConfig = (Map<String, Object>) easyMqConfig.get("idempotent");
            if (idempotentConfig != null) {
                log.info("   幂等控制配置 - enabled: {}, storage: {}", 
                        idempotentConfig.get("enabled"), idempotentConfig.get("storage"));
            }
            
            // 检查监控配置
            @SuppressWarnings("unchecked")
            Map<String, Object> monitorConfig = (Map<String, Object>) easyMqConfig.get("monitor");
            if (monitorConfig != null) {
                log.info("   监控配置 - enabled: {}, storage: {}", 
                        monitorConfig.get("enabled"), monitorConfig.get("storage"));
            }
        }
    }
    
    /**
     * 演示配置属性使用
     */
    private void demonstratePropertiesUsage() {
        log.info("2. 使用配置属性...");
        
        log.info("   SDK启用状态: {}", easyMqProperties.isEnabled());
        
        // 幂等控制配置
        var idempotent = easyMqProperties.getIdempotent();
        log.info("   幂等控制 - 启用: {}, 存储类型: {}", 
                idempotent.isEnabled(), idempotent.getStorage());
        
        if ("caffeine".equals(idempotent.getStorage())) {
            var caffeine = idempotent.getCaffeine();
            log.info("   Caffeine配置 - 最大大小: {}, 过期时间: {}秒", 
                    caffeine.getMaximumSize(), caffeine.getExpireAfterWrite());
        }
        
        // 监控配置
        var monitor = easyMqProperties.getMonitor();
        log.info("   监控配置 - 启用: {}, 存储类型: {}", 
                monitor.isEnabled(), monitor.getStorage());
        
        if ("sqlite".equals(monitor.getStorage())) {
            var sqlite = monitor.getSqlite();
            log.info("   SQLite配置 - 数据库路径: {}, 最大记录数: {}, 保留天数: {}", 
                    sqlite.getDbPath(), sqlite.getMaxRecords(), sqlite.getRetentionDays());
        }
        
        // Web控制台配置
        var console = easyMqProperties.getConsole();
        log.info("   Web控制台 - 启用: {}, 端口: {}, 上下文路径: {}", 
                console.isEnabled(), console.getPort(), console.getContextPath());
        
        // 序列化配置
        var serialization = easyMqProperties.getSerialization();
        log.info("   序列化配置 - 类型: {}", serialization.getType());
        
        if ("json".equals(serialization.getType())) {
            var json = serialization.getJson();
            log.info("   JSON配置 - 日期格式: {}, 时区: {}", 
                    json.getDateFormat(), json.getTimeZone());
        }
    }
    
    /**
     * 演示配置优先级
     */
    private void demonstrateConfigPriority() {
        log.info("3. 配置优先级说明...");
        
        log.info("   配置加载优先级（从高到低）:");
        log.info("   1. 主应用配置文件 (application.yml, application-{profile}.yml)");
        log.info("   2. 环境变量 (EASY_MQ_*)");
        log.info("   3. 系统属性 (-Deasy-mq.*)");
        log.info("   4. SDK默认配置文件 (application-easymq.yml)");
        log.info("   5. 代码默认值 (EasyMqProperties中的默认值)");
        
        log.info("   设计理念:");
        log.info("   - SDK只提供默认配置，确保开箱即用");
        log.info("   - 环境配置由主应用管理，符合SDK设计原则");
        log.info("   - 主应用配置会覆盖SDK默认配置");
        log.info("   - 所有配置都有合理的默认值，确保功能可用");
    }
} 